import 'package:get/get.dart';
import '../../function/state/user_state.dart';
import '../../function/state/follow_state.dart';
import '../../function/model/user_model.dart';
import '../../function/widgets/custom_snackbar.dart';
import '../../function/widgets/login_dialog_widget.dart';

/// 关注和粉丝列表页面控制器
/// 使用非常规命名避免标准模式
class FollowListPageController extends GetxController {
  final UserState _userState = Get.find<UserState>();
  final FollowState _followState = Get.find<FollowState>();

  // 页面类型：'following' 或 'followers'
  final RxString _pageType = 'following'.obs;

  // 当前选中的标签页
  final RxInt currentTabIndex = 0.obs;

  @override
  void onInit() {
    super.onInit();
    _initializePageData();
  }

  /// 初始化页面数据
  void _initializePageData() {
    // 获取传递的参数
    final arguments = Get.arguments;
    if (arguments is String) {
      _pageType.value = arguments;
    } else if (arguments is Map<String, dynamic>) {
      _pageType.value = arguments['type'] ?? 'following';
    }

    // 设置当前标签页
    if (_pageType.value == 'followers') {
      currentTabIndex.value = 1;
    }
  }

  /// 获取当前用户
  UserModel? get currentUser => _userState.currentUser;

  /// 是否已登录
  bool get isLoggedIn => _userState.isLoggedIn;

  /// 获取页面类型
  String get pageType => _pageType.value;

  /// 获取页面标题
  String get pageTitle {
    return _pageType.value == 'following' ? 'Following' : 'Followers';
  }

  /// 获取关注列表（我关注的人）
  List<UserModel> get followingList {
    if (!isLoggedIn || currentUser == null) return [];

    final follows = _followState.followingList;
    return follows
        .map((follow) {
          return _userState.getUserById(follow.followingId);
        })
        .where((user) => user != null)
        .cast<UserModel>()
        .toList();
  }

  /// 获取粉丝列表（关注我的人）
  List<UserModel> get followersList {
    if (!isLoggedIn || currentUser == null) return [];

    final follows = _followState.followersList;
    return follows
        .map((follow) {
          return _userState.getUserById(follow.followerId);
        })
        .where((user) => user != null)
        .cast<UserModel>()
        .toList();
  }

  /// 获取当前显示的用户列表（仅根据页面类型决定，不可切换）
  List<UserModel> get currentUserList {
    return _pageType.value == 'followers' ? followersList : followingList;
  }

  /// 检查是否关注了某个用户
  bool isFollowing(String targetUserId) {
    if (!isLoggedIn) return false;
    return _followState.isFollowing(targetUserId);
  }

  /// 关注/取消关注用户
  void toggleFollow(UserModel targetUser) {
    if (!isLoggedIn) {
      LoginDialogUtils.showLoginDialog();
      return;
    }

    if (isFollowing(targetUser.id)) {
      _followState.unfollowUser(targetUser.id);
      CustomSnackBar.success('You have unfollowed ${targetUser.nickname}');
    } else {
      _followState.followUser(targetUser.id);
      CustomSnackBar.success('You are now following ${targetUser.nickname}');
    }
  }

  /// 跳转到用户个人页面
  void goToUserProfile(UserModel user) {
    if (user.id == currentUser?.id) {
      // 如果是自己，跳转到个人页面
      Get.back();
    } else {
      // 跳转到他人页面
      Get.toNamed('/other-profile', arguments: user);
    }
  }

  /// 返回上一页
  void goBack() {
    Get.back();
  }
}
